package com.panwenliang2008.eatwhat.ui.suggestion

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.google.android.material.snackbar.Snackbar
import com.google.firebase.analytics.FirebaseAnalytics
import com.panwenliang2008.eatwhat.R
import kotlinx.android.synthetic.main.fragment_suggestion.*
import kotlinx.android.synthetic.main.fragment_suggestion.view.*

class SuggestionFragment : Fragment() {

    private val TAG = "SuggestionFragment"

    private lateinit var slideshowViewModel: SuggestionViewModel

    // [START declare_analytics]
    private var mFirebaseAnalytics: FirebaseAnalytics? = null
    // [END declare_analytics]

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Obtain the FirebaseAnalytics instance.
        mFirebaseAnalytics = FirebaseAnalytics.getInstance(requireActivity())
        // [END shared_app_measurement]

        // [START set_current_screen]
        mFirebaseAnalytics?.setCurrentScreen(requireActivity(), TAG, null)
        // [END set_current_screen]
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        slideshowViewModel = ViewModelProvider(this).get(SuggestionViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_suggestion, container, false)

        root.editText.addTextChangedListener {
            when {
                root.editText.length() == 0 -> {
                    // Set error text
                    root.outlinedTextField.error = getString(R.string.suggestion_message_006)
                }
                root.editText.length() > 100 -> {
                    // Set error text
                    root.outlinedTextField.error = getString(R.string.suggestion_message_005)
                }
                else -> {
                    // Clear error text
                    root.outlinedTextField.error = null
                }
            }
        }

        root.buttonOk.setOnClickListener {
            when {
                root.editText.length() == 0 -> {
                    // Set error text
                    root.outlinedTextField.error = getString(R.string.suggestion_message_006)
                    Snackbar.make(it, R.string.suggestion_message_006, Snackbar.LENGTH_SHORT).show()
                }
                root.editText.length() > 100 -> {
                    Snackbar.make(it, R.string.suggestion_message_005, Snackbar.LENGTH_SHORT).show()
                }
                else -> {
                    val bundle = Bundle()
                    bundle.putString("suggestionContent", editText.text.toString())
                    mFirebaseAnalytics?.logEvent("suggestion", bundle)

                    Snackbar.make(it, R.string.app_name, Snackbar.LENGTH_SHORT).show()
                }
            }
        }

        return root
    }
}
